<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
                    <el-form-item label="检测数据的id" prop="checkDataId">
                      <el-input v-model="formData.checkDataId" placeholder="请输入检测数据的id" />
                    </el-form-item>
                    <el-form-item label="进货价格" prop="purchasingPrice">
                      <el-input v-model="formData.purchasingPrice" placeholder="请输入进货价格" />
                    </el-form-item>
                    <el-form-item label="销售价格" prop="salePrice">
                      <el-input v-model="formData.salePrice" placeholder="请输入销售价格" />
                    </el-form-item>
                    <el-form-item label="进货重量" prop="purchasingWeight">
                      <el-input v-model="formData.purchasingWeight" placeholder="请输入进货重量" />
                    </el-form-item>
                    <el-form-item label="商品产地" prop="productFrom">
                      <el-input v-model="formData.productFrom" placeholder="请输入商品产地" />
                    </el-form-item>
                    <el-form-item label="进货时间" prop="productFromDate">
                      <el-date-picker clearable v-model="formData.productFromDate" type="date" value-format="timestamp" placeholder="选择进货时间" />
                    </el-form-item>
                    <el-form-item label="供货人" prop="supplierName">
                      <el-input v-model="formData.supplierName" placeholder="请输入供货人" />
                    </el-form-item>
                    <el-form-item label="交易地点" prop="transactionPlace">
                      <el-input v-model="formData.transactionPlace" placeholder="请输入交易地点" />
                    </el-form-item>
                    <el-form-item label="数据来源id" prop="dataSourceId">
                      <el-select v-model="formData.dataSourceId" placeholder="请选择数据来源id">
                            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.DATA_SOURCE_TYPE)"
                                       :key="dict.value" :label="dict.label" :value="parseInt(dict.value)" />
                      </el-select>
                    </el-form-item>
                    <el-form-item label="合格图片" prop="imgs">
                      <el-input v-model="formData.imgs" placeholder="请输入合格图片" />
                    </el-form-item>
      </el-form>
              <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import * as CheckDataExtendApi from '@/api/market/checkdataextend';
      export default {
    name: "CheckDataExtendForm",
    components: {
                    },
    data() {
      return {
        // 弹出层标题
        dialogTitle: "",
        // 是否显示弹出层
        dialogVisible: false,
        // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
        formLoading: false,
        // 表单参数
        formData: {
                            id: undefined,
                            checkDataId: undefined,
                            purchasingPrice: undefined,
                            salePrice: undefined,
                            purchasingWeight: undefined,
                            productFrom: undefined,
                            productFromDate: undefined,
                            supplierName: undefined,
                            transactionPlace: undefined,
                            dataSourceId: undefined,
                            imgs: undefined,
        },
        // 表单校验
        formRules: {
        },
                        };
    },
    methods: {
      /** 打开弹窗 */
     async open(id) {
        this.dialogVisible = true;
        this.reset();
        // 修改时，设置数据
        if (id) {
          this.formLoading = true;
          try {
            const res = await CheckDataExtendApi.getCheckDataExtend(id);
            this.formData = res.data;
            this.title = "修改商品详情";
          } finally {
            this.formLoading = false;
          }
        }
        this.title = "新增商品详情";
              },
      /** 提交按钮 */
      async submitForm() {
        // 校验主表
        await this.$refs["formRef"].validate();
                  this.formLoading = true;
        try {
          const data = this.formData;
                  // 修改的提交
          if (data.id) {
            await CheckDataExtendApi.updateCheckDataExtend(data);
            this.$modal.msgSuccess("修改成功");
            this.dialogVisible = false;
            this.$emit('success');
            return;
          }
          // 添加的提交
          await CheckDataExtendApi.createCheckDataExtend(data);
          this.$modal.msgSuccess("新增成功");
          this.dialogVisible = false;
          this.$emit('success');
        } finally {
          this.formLoading = false;
        }
      },
                      /** 表单重置 */
      reset() {
        this.formData = {
                            id: undefined,
                            checkDataId: undefined,
                            purchasingPrice: undefined,
                            salePrice: undefined,
                            purchasingWeight: undefined,
                            productFrom: undefined,
                            productFromDate: undefined,
                            supplierName: undefined,
                            transactionPlace: undefined,
                            dataSourceId: undefined,
                            imgs: undefined,
        };
        this.resetForm("formRef");
      }
    }
  };
</script>
